<?php
/**
 * Created by PhpStorm.
 * User: 张辉
 * Date: 2018/8/6
 * Time: 12:58
 */
namespace app\common\redis;
/*
 * 安卓push
 */
use app\common\model\article\PublishArticleModel;
use app\common\model\coin\CoinRuntimeModel;

class PythonPushRedis extends RedisModel
{
    /**
     * Redis配置名称
     * @var string
     */
    public $redis = 'coinExchangeData';

    /**
     * Redis key
     * @var string
     */
    public $key = 'list:python_redis_push_key';

    public $coin_article_hot_key = "zset:coin_article_hot";


    // 24h成交额数据
    public static $rank_24h_volume_key = "rank_24h_volume";

    // 24h小时涨跌幅数据
    public static $global_ticker_price_raise_24h_key = "global_ticker_price_raise_24h";


    // 获取top200涨跌幅
    public function get_global_ticker_price_raise_24h_top_200()
    {
        $rank_24h_volume_list = $this->SortSetZRange( 0, -1, false, PythonPushRedis::$global_ticker_price_raise_24h_key  );
        $rank_24h_volume_list_format = array();

        $CoinRuntimeModel = CoinRuntimeModel::getInstance();

        $where = [
            'status' => 1,
            'type' => 1,
            'sort' => array('gt', 0),
            'deleted' => -1,
        ];

        $coin_id_list = $CoinRuntimeModel->search_id_list( $where, $page = 1, $asc = true, $sort_by = 'sort', $list_rows = 200 );

        foreach ( $rank_24h_volume_list as $value ){
            $info = json_decode( $value, true );
            if ( !in_array( $info['token_id'], $coin_id_list ) ){
                continue;
            }
            $rank_24h_volume_list_format[] = $info;
        }

        return $rank_24h_volume_list_format;
    }

    // 币种文章热度排行
    public function coin_article_hot()
    {
        $start_time = time() - 86400;
        $end_time = time();
        // 获取昨天文章数统计
        $today_list = $this->get_coin_article_count( $start_time, $end_time );

        // 获取昨天文章数统计
        $yesterday_list = $this->get_coin_article_count( $start_time - 86400 , $start_time );
        $count_list = array();

        foreach ( $today_list as $key => $value ){
            // 若今日文章数小于3
            if ( $value < 3 ){
                continue;
            }

            if ( !isset( $yesterday_list[$key] ) ){
                $count_list[$key] = $value * 100;
                continue;
            }

            $count_list[$key] = bcmul( bcdiv( $value - $yesterday_list[$key], $yesterday_list[$key] , 4 ), 100 );
        }

        $this->Delete( $this->coin_article_hot_key );

        foreach ( $count_list as $key => $value ){
            $this->SortSetZadd( $key, $value, $this->coin_article_hot_key );
        }

        return $count_list;
    }

    // 获取一段时间内币种文章统计
    private function get_coin_article_count( $start_time, $end_time )
    {
        $PublishArticleModel = PublishArticleModel::getInstance();

        $where = array(
            "create_time" => array(
                'between', array(
                    date( 'Y-m-d H:i:s', $start_time ),
                    date( 'Y-m-d H:i:s', $end_time ),
                )
            ),

            "is_tmp"   => -1,
            "article_type" => array( "neq", PublishArticleModel::CMS_TYPE_DATADAILY )
        );

        // 获取今日币种文章数
        $day_list = $PublishArticleModel->get_key_value( "id", "coin_list" , $where );

        $day_list_format = array();

        foreach ( $day_list as $value ){
            $coin_list = json_decode( $value ,true );
            if ( $coin_list ){
                foreach ( $coin_list as $v ){
                    $day_list_format[$v] ++;
                }
            }
        }

        return $day_list_format;
    }















}